﻿/*************************************************************************
 * Copyright 2013, OhZeeCreative, LLC
 * This code is protected by copyright. Unauthorized use is prohibited.
 *************************************************************************/

namespace OhZeeCreative.Helpers
{
    using System;
    using System.Threading.Tasks;
    using Windows.ApplicationModel.Store;
    using Windows.Foundation;

    public static class StoreHelper
    {
        public static Uri GetAppWebPage(string appGuid)
        {
            // appGuid can be retrieved from the Dashboard's Detail page under the title "Link to ..."
            return new Uri("http://apps.microsoft.com/webpdp/app/" + appGuid);
        }

        public static Uri GetWindowsPhoneAppWebPage(string appGuid)
        {
            // Use this for Windows Phone
            return new Uri("http://www.windowsphone.com/s?appid=" + appGuid);
        }

        public static Uri GetInfoPage(string packageFamilyName)
        {
            // packageFamilyName can be retrieved through Package.appxmanifest
            return new Uri("ms-windows-store:PDP?PFN=" + packageFamilyName);
        }

        public static Uri GetRatingPage(string packageFamilyName)        
        {
            // packageFamilyName can be retrieved through Package.appxmanifest
            return new Uri("ms-windows-store:REVIEW?PFN=" + packageFamilyName);
        }

        public static Uri GetSearchPage(string query)
        {
            return new Uri("ms-windows-store:SEARCH?query=" + query);
        }


        public static bool IsProductActive(string productName)
        {
            LicenseInformation licInfo;

#if DEBUG
            licInfo = CurrentAppSimulator.LicenseInformation;
#else
            licInfo = CurrentApp.LicenseInformation;
#endif

            try
            {
                ProductLicense productLicense = licInfo.ProductLicenses[productName];
                return productLicense.IsActive;
            }
            catch
            {
                DebugHelper.Fail();
                return false;
            }
        }

        public static async Task<bool> PurchaseInAppItem(string productName, bool includeReceipt = false)
        {
            if (IsProductActive(productName))
            {
                DebugHelper.Fail();
                return false;
            }

            try
            {
#if DEBUG
                IAsyncOperation<string> purchaseOperation;
                purchaseOperation = CurrentAppSimulator.RequestProductPurchaseAsync(productName, includeReceipt);
                await purchaseOperation;
#else
                await CurrentApp.RequestProductPurchaseAsync(productName, includeReceipt);
#endif
            }
            catch (Exception ex)
            {
                DebugHelper.Fail(ex);
                return false;
            }

#if DEBUG
            return true;
#else
            return IsProductActive(productName);
#endif
        }

        //public static void PurchaseInApItemCompletedHandler<T>(IAsyncOperation<T> asyncInfo, AsyncStatus asyncStatus)
        //{
        //}
    }
}
